package com.cqjtu.graduation.controller;


import com.cqjtu.graduation.entity.DinnerTableDO;
import com.cqjtu.graduation.entity.FoodTypeDO;
import com.cqjtu.graduation.service.DinnerTableDOService;
import com.cqjtu.graduation.service.FoodTypeDOService;
import com.cqjtu.graduation.util.FileServerCommon;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 与菜品相关的控制器
 * @author tianya
 */
@Controller
@RequestMapping("/foodtype")
public class FoodTypeController extends BaseController {
    @Autowired
    private FoodTypeDOService foodTypeDOService;
    @Autowired
    private DinnerTableDOService dinnerTableDOService;

    @RequestMapping("/listFoodType")
    public String listFoodType(HttpServletRequest request){
        init(request);
        FoodTypeDO foodTypeDO = new FoodTypeDO();
        foodTypeDO.setStart(getStart());
        foodTypeDO.setLength(PAGE_NUM);
        Long total = foodTypeDOService.countFoodTypeDO();
        List<FoodTypeDO> listFoodType = foodTypeDOService.listFoodTypeInfo(foodTypeDO);
        List<DinnerTableDO> tables = dinnerTableDOService.listDinnerDO();
        request.setAttribute("listFoodType",listFoodType);
        request.setAttribute("total",total);
        request.setAttribute("tables",tables);
        return "OrderSys/admin/LookClassOfFood";
    }
    @RequestMapping("/findAllFoodType")
    public String findAllFoodType(HttpServletRequest request){
        init(request);
        FoodTypeDO foodTypeDO = new FoodTypeDO();
        foodTypeDO.setStart(getStart());
        foodTypeDO.setLength(PAGE_NUM);
        Long total = foodTypeDOService.countFoodTypeDO();
        List<FoodTypeDO> listFoodType = foodTypeDOService.listFoodTypeInfo(foodTypeDO);
        System.out.println(listFoodType);
//        List<DinnerTableDO> tables = dinnerTableDOService.listDinnerDO();
        request.setAttribute("listFoodType",listFoodType);
        request.setAttribute("total",total);
//        request.setAttribute("tables",tables);
        return "OrderSys/admin/BrowseDishes";
    }

    @RequestMapping("/deleteFoodType")
    public String deleteFoodType(HttpServletRequest request,Integer id){
        foodTypeDOService.deleteFoodTypeInfo(id);
        return findAllFoodType(request);
    }
    @RequestMapping("/updataFoodType")
    public String updataFoodType(FoodTypeDO foodTypeDO){
        FoodTypeDO tempfoodtype=foodTypeDOService.findOneFoodTypeInfo(foodTypeDO.getFoodtypeId());
        foodTypeDO.setFoodtypePicturePath(tempfoodtype.getFoodtypePicturePath());
        foodTypeDOService.updataFoodTypeInfo(foodTypeDO);
        return "OrderSys/admin/BrowseDishes";
    }

    @RequestMapping("/saveFoodType")
    public  String saveFoodType(@RequestParam(value="file", required = false) MultipartFile imagefile,
                                 @RequestParam(value="name",required = false) String name,
                                 @RequestParam(value="introduction",required = false) String introduction,
                                 @RequestParam(value="detail",required = false) String detail,
                                 @RequestParam(value="isrecommend",required = false) int isrecommend,
                                 @RequestParam(value="price",required = false) float price) throws Exception{
        //上传图片部分
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = df.format(new Date());
        format = format + new Random().nextLong();
        String url = FileServerCommon.FILE_SERVER_HOST + format + ".jpg";//要将图片保存到网络上的完整地址
        String partUrl = format + ".jpg";// 部分地址  /xxx.jpg
        //创建Jersey框架的客户端
        Client client = new Client();
        WebResource resource = client.resource(url);
        try {
            byte[] buf;
            buf = imagefile.getBytes();
            resource.put(String.class,buf);//通过put方式向网络url发送资源
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将数据存入数据库
        FoodTypeDO foodTypeDO=new FoodTypeDO();
        foodTypeDO.setFoodtypeName(name);
        foodTypeDO.setFoodtypeIntroduction(introduction);
        foodTypeDO.setFoodtypeDetail(detail);
        foodTypeDO.setFoodtypeIsrecommend(isrecommend);
        foodTypeDO.setFoodtypePrice(price);
        foodTypeDO.setFoodtypePicturePath(url);
        foodTypeDOService.saveFoodTypeInfo(foodTypeDO);
        return "OrderSys/admin/BrowseDishes";
    }


}
